<div id="content">
<h1>Tournament Signup</h1>
<?php
	$id = REPLACE_Injection($_GET["id"]);
	$query = new mysqlQuery("tournament", "", "torneo", "", "`id`='".$id."'", "", "");
	$query->execute();
	if($query->getCount() == 1){		
		$rowTournament = $query->next();
		$query = new mysqlQuery("tournament", "", "jugadores", "", "`torneo`='".$id."' AND (`jugador1`='".$_SESSION["user_id"]."' OR `jugador2`='".$_SESSION["user_id"]."')", "", "");
		$query->execute();
		if($query->getCount() == 0){
			if(obtenerContinenteTorneo($rowTournament["continente"]) == $_SESSION["continent"] || $rowTournament["continente"] == 0){
				switch($rowTournament["tipo"]){
					case 1: 	$values = array(NULL, $_SESSION["user_id"], 0, 0, $id, time(), 1); 
									$ok = "You will receive a notification when the tournament will start including the information regarding the random team you will play with.";
									break;
					case 2: 
									if(!$couple = REPLACE_Injection($_POST["couple"])){
										$error = "You have to write the name of the person that will be your couple";
									}else{
										$query = new mysqlQuery("tournament", "", "usuarios", "", "UPPER(`usuario`) = UPPER('".$couple."')", "", "");
										$query->execute();
										if($query->getCount()){
											$row = $query->next();
											if(obtenerContinenteTorneo($rowTournament["continente"]) == $row["continent"]){
												$nombreJugadorDestino = $row["nombre"];
												$idJugadorDestino = $row["id"];
												$query = new mysqlQuery("tournament", "", "jugadores", "", "`torneo`='".$id."' AND (`jugador1`='".$row["id"]."' OR `jugador2`='".$row["id"]."')", "", "");
												$query->execute();
												if($query->getCount() == 0){
													$values = array(NULL, $_SESSION["user_id"], $row["id"], 0, $id, time(), 0);
													crearNotificacion($_SESSION["user_id"], $_SESSION["username"], $idJugadorDestino, $nombreJugadorDestino, $id);
													$ok = "You have been registered for the tournament, your couple will receive a notification.<br/>You will receive a notification when the tournament will start including the information regarding the random team you will play with.";
												}else{
													$error = "The user you want to play with is already registered with another player for the tournament.";
												}
											}else{
												$error = "The username entered is not from the suitable continent in order to play this tournament with you.";
											}
										}else{
											$error = "The username of the couple you have entered does not exist in the Tournament dabase. <br/>
															If he is registered in the forum, say him to access the PES6J Tournament website.";
										}
									}
									break;
									
					case 3:	$a = obtenerSelectEquipoTorneo($rowTournament["equipos"], FALSE);
									$equipo = REPLACE_Injection($_POST["team"]);
									if(array_search($equipo, $a)){
										$values = array(NULL, $_SESSION["user_id"], 0, $equipo, $id, time(), 1);
										$ok = "You have been registered in the tournament.";
									}else{
										$error = "The team you want to play with is not included in the valid team range for this tournament.";
									}
									break;
									
					case 4:	if(!$couple = REPLACE_Injection($_POST["couple"])){
										$error = "You have to write the name of the person that will be your couple";
									}else{
										$query = new mysqlQuery("tournament", "", "usuarios", "", "UPPER(`nombre`) = UPPER('".$couple."')", "", "");
										$query->execute();
										if($query->getCount()){
											$row = $query->next();
											$nombreJugadorDestino = $row["nombre"];
											$idJugadorDestino = $row["id"];
											if(obtenerContinenteTorneo($rowTournament["continente"]) == $row["continent"]){
												$query = new mysqlQuery("tournament", "", "jugadores", "", "`torneo`='".$id."' AND (`jugador1`='".$row["id"]."' OR `jugador2`='".$row["id"]."')", "", "");
												$query->execute();
												if($query->getCount() == 0){
													$a = obtenerSelectEquipoTorneo($rowTournament["equipos"], FALSE);
													$equipo = REPLACE_Injection($_POST["team"]);
													if(array_search($equipo, $a)){
														$values = array(NULL, $_SESSION["user_id"], $row["id"], $equipo, $id, time(), 1);
														crearNotificacion($_SESSION["user_id"], $_SESSION["username"], $idJugadorDestino, $nombreJugadorDestino, $id);
														$ok = "You have been registered in the tournament.";
													}else{
														$error = "The team you want to play with is not included in the valid team range for this tournament.";
													}
												}else{
													$error = "The user you want to play with is already registered with another player for the tournament.";
												}
											}else{
												$error = "The username entered is not from the suitable continent in order to play this tournament with you.";
											}
										}else{
											$error = "The username of the couple you have entered does not exist in the Tournament dabase. <br/>
															If he is registered in the forum, say him to access the PES6J Tournament website.";
										}
									}
									break;
									
					case 5: 	$values = array(NULL, $_SESSION["user_id"], 0, 0, $id, time(), 1); 
									$ok = "You will receive a notification when the tournament will start including the information regarding the random team you will play with.";
									break;
				}
				if(strlen($error) == 0){
					$insert = new mysqlInsert("tournament", "", "jugadores", $values);
					$insert->execute();
					$u = "UPDATE `torneo` SET `inscripciones`= inscripciones + 1 WHERE `id`='".$id."'";
					$update = new mysqlUpdate("tournament", $u, "", "", "", "");
					$update->execute();
					echo '<div id="ok">'.$ok.'</div>';
				}else{
					echo '<div id="error">'.$error.'</div>';
				}				
			}else{
				echo '<div id="error">You are not able to sign up for the tormanent because you are from another continent</div>';
			}
		}else{
			echo '<div id="error">You are already registered in the tournament</div>';
		}
	}else{
		echo '<div id="error">The tournament does not exist</div>';
	}
?>
</div>